// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License/free.pdf

//----------------------------------------------------------------------------
template <class T>
MgcTList<T>::MgcTList ()
{
    m_uiQuantity = 0;
    m_pkFront = 0;
    m_pkIterator = 0;
}
//----------------------------------------------------------------------------
template <class T>
MgcTList<T>::~MgcTList ()
{
    delete m_pkFront;
}
//----------------------------------------------------------------------------
template <class T>
unsigned int MgcTList<T>::GetQuantity () const
{
    return m_uiQuantity;
}
//----------------------------------------------------------------------------
template <class T>
void MgcTList<T>::Add (T tValue)
{
    Node* pkNode = new Node(tValue,m_pkFront);
    m_pkFront = pkNode;
    m_uiQuantity++;
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::AddUnique (T tValue)
{
    Node* pkSearch = m_pkFront;
    while ( pkSearch )
    {
        if ( pkSearch->m_tValue == tValue )
            return false;
        pkSearch = pkSearch->m_pkNext;
    }

    Add(tValue);
    return true;
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::Remove (T tValue)
{
    if ( m_pkFront )
    {
        if ( m_pkFront->m_tValue == tValue )
        {
            // item at beginning of list
            Node* pkSave = m_pkFront;
            m_pkFront = m_pkFront->m_pkNext;
            pkSave->m_pkNext = 0;
            delete pkSave;
            m_uiQuantity--;
            return true;
        }

        // search for item
        Node* pkPrev = m_pkFront;
        Node* pkCurr = m_pkFront->m_pkNext;
        while ( pkCurr && pkCurr->m_tValue != tValue )
        {
            pkPrev = pkCurr;
            pkCurr = pkCurr->m_pkNext;
        }
        if ( pkCurr )
        {
            // found the item
            Node* pkSave = pkCurr;
            pkPrev->m_pkNext = pkCurr->m_pkNext;
            pkSave->m_pkNext = 0;
            delete pkSave;
            m_uiQuantity--;
            return true;
        }
    }

    return false;
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::RemoveFront (T& rtValue)
{
    if ( m_pkFront )
    {
        rtValue = m_pkFront->m_tValue;

        Node* pkSave = m_pkFront;
        m_pkFront = m_pkFront->m_pkNext;
        pkSave->m_pkNext = 0;
        delete pkSave;
        m_uiQuantity--;
        return true;
    }
    else
    {
        return false;
    }
}
//----------------------------------------------------------------------------
template <class T>
void MgcTList<T>::RemoveAll ()
{
    delete m_pkFront;
    m_pkFront = 0;
    m_uiQuantity = 0;
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::GetFirst (T& rtValue)
{
    if ( m_pkFront )
    {
        rtValue = m_pkFront->m_tValue;
        m_pkIterator = m_pkFront->m_pkNext;
        return true;
    }
    else
    {
        m_pkIterator = 0;
        return false;
    }
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::GetNext (T& rtValue)
{
    if ( m_pkIterator )
    {
        rtValue = m_pkIterator->m_tValue;
        m_pkIterator = m_pkIterator->m_pkNext;
        return true;
    }
    else
    {
        return false;
    }
}
//----------------------------------------------------------------------------
template <class T>
void MgcTList<T>::ReverseOrder ()
{
    Node* pkReverse = 0;

    while ( m_pkFront )
    {
        Node* pkNode = m_pkFront;
        m_pkFront = m_pkFront->m_pkNext;
        pkNode->m_pkNext = pkReverse;
        pkReverse = pkNode;
    }

    m_pkFront = pkReverse;
}
//----------------------------------------------------------------------------
template <class T>
bool MgcTList<T>::Contains (T tValue) const
{
    Node* pkSearch = m_pkFront;

    while ( pkSearch )
    {
        if ( pkSearch->m_tValue == tValue )
            return true;

        pkSearch = pkSearch->m_pkNext;
    }

    return false;
}
//----------------------------------------------------------------------------
